package impl.sort;

import anaydis.sort.SorterType;

import java.util.Comparator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tomi
 * Date: 08/09/2011
 * Time: 17:02:32
 * To change this template use File | Settings | File Templates.
 */
public class HSorter extends AbstractSorter {
    public HSorter() {
        super(SorterType.H);
    }
    protected HSorter(final SorterType type) {
        super(type);
    }
    public <T> void sort(Comparator<T> c, List<T> list) {
        sort(c, list, (int)(list.size()/2));
    }
    protected <T> void sort(Comparator<T> c, List<T> list, int h){
        if((h>=1)&&(h<=list.size())){
            for(int i=0; i<h; i++){
                int swaps = 0;
                for(int j=i; j+h<list.size(); j=j+h){
                    if(greater(c, list, j, j+h)){
                        swap(list, j, j+h);
                        swaps++;
                    }
                }
                if(swaps!=0){
                    i--;
                }
            }
        }
    }
}